package com.shop.admin.controller;

import com.shop.Result.LdbzResult;
import com.shop.api.feign.AdminAuthFeign;
import com.shop.pojo.LdbzAdminAuth;
import com.shop.utils.ConvertUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/admin/adminAuth")
public class AdminAuthController {

    private static final Logger logger = LoggerFactory.getLogger(AdminAuthController.class);

    @Autowired
    private AdminAuthFeign adminAuthFeign;

    @ApiOperation(value = "用户页面跳转",notes = "跳转到该角色下所有用户的页面")
    @RequestMapping(value = "/user/{roleId}",method = RequestMethod.GET)
    public String adminAuthUser(@PathVariable("roleId") Long roleId , Model m){
        logger.debug("go to adminAuthUser {}",roleId);
        m.addAttribute("roleId",roleId);
        return "adminAuthUser";
    }

    @ApiOperation(value = "资源页面跳转",notes = "跳转到该角色下所有资源的页面")
    @RequestMapping(value = "/menu/{roleId}",method = RequestMethod.GET)
    public String adminAuthMenu(@PathVariable("roleId") Long roleId,Model m){
        logger.debug("go to adminAuthMenu {}",roleId);
        m.addAttribute("roleId",roleId);
        return "adminAuthMenu";
    }

    @ApiOperation(value="获取用户", notes="根据该角色ID中有哪些用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "page", value = "查询的页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页显示的条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "realName", value = "用户的姓名", dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value="/selectUserByRole" , method = RequestMethod.POST)
    public LdbzResult selectUserByRole(Long roleId,int page,int limit,
                                       @RequestParam(value = "realName",defaultValue = "") String realName){
        return adminAuthFeign.selectUserByRole(roleId,page,limit, ConvertUtils.getKey(realName));
    }

    @ApiOperation(value="获取资源", notes="根据该角色ID中有哪些资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "page", value = "查询的页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页显示的条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "menuName", value = "资源的名字", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value="/selectMenuByRole" , method = RequestMethod.POST)
    public LdbzResult selectMenuByRole(Long roleId,int page,int limit,
                                       @RequestParam(value = "menuName",defaultValue = "") String menuName){
        return adminAuthFeign.selectMenuByRole(roleId,page,limit,ConvertUtils.getKey(menuName));
    }

    @ApiOperation(value="删除角色下的用户", notes="物理删除一条改角色下的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "int")
    })
    @ResponseBody
    @RequestMapping(value="/deleteByUser" , method = RequestMethod.POST)
    public LdbzResult deleteByUser(Long userId,Long roleId){
        return adminAuthFeign.deleteByUser(userId,roleId);
    }

    @ApiOperation(value="删除角色下的资源", notes="物理删除一条改角色下的资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "资源id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "int")
    })
    @ResponseBody
    @RequestMapping(value="/deleteByMenu" , method = RequestMethod.POST)
    public LdbzResult deleteByMenu(Long menuId,Long roleId){
        return adminAuthFeign.deleteByMenu(menuId,roleId);
    }

    @ApiOperation(value="角色分配用户", notes="给指点的角色分配用户")
    @ApiImplicitParam(name = "entity", value = "LdbzAdminAuth实体", required = true, dataType = "LdbzAdminAuth")
    @ResponseBody
    @RequestMapping(value="/insertByUser" , method = RequestMethod.POST)
    public LdbzResult insertByUser(LdbzAdminAuth entity){
        entity.setCreated(new Date());
        return adminAuthFeign.insertByUser(entity);
    }

    @ApiOperation(value="角色分配资源", notes="给指点的角色分配资源")
    @ApiImplicitParam(name = "entity", value = "LdbzAdminAuth实体", required = true, dataType = "LdbzAdminAuth")
    @ResponseBody
    @RequestMapping(value="/insertByMenu" , method = RequestMethod.POST)
    public LdbzResult insertByMenu(LdbzAdminAuth entity){
        entity.setCreated(new Date());
        return adminAuthFeign.insertByMenu(entity);
    }

    @ApiOperation(value="获取用户信息", notes="根据用户的账号或者名字获取账号信息")
    @ApiImplicitParam(name = "queryKey", value = "用户的账号或者名字", required = true, dataType = "string")
    @ResponseBody
    @RequestMapping(value="/getUserByNameOrAccount" , method = RequestMethod.POST)
    public LdbzResult getUserByNameOrAccount(String queryKey){
        return adminAuthFeign.getUserByNameOrAccount(ConvertUtils.getKey(queryKey));
    }

    @ApiOperation(value="获取资源信息", notes="根据资源的名称或请求路径获取资源信息")
    @ApiImplicitParam(name = "queryKey", value = "资源的名称或请求路径", required = true, dataType = "string")
    @ResponseBody
    @RequestMapping(value="/getResByNameOrUrl" , method = RequestMethod.POST)
    public LdbzResult getResByNameOrUrl(String queryKey){
        return adminAuthFeign.getResByNameOrUrl(ConvertUtils.getKey(queryKey));
    }
}
